package racing.stage;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * A Factory class for creating the racing Stage.
 * 
 * @author Sarathit Sangtaweep 5510546182
 * @author Veerapat Teeravipark 5510547022
 * @version 2013
 */
public class StageFactory {
	/** All stage. */
	private static Map<String, Integer[][]> mapStage;
	static {
		mapStage = new HashMap<String, Integer[][]>();
		mapStage.put("Racing Field", createRectangle(10, 20));
		mapStage.put("Midnight Field", createRectangle(20, 20));
		mapStage.put("Road To Heaven", createRectangle(18, 23));
	}
	/** Factory object. */
	private static StageFactory instance = new StageFactory();

	/**
	 * Constructor of the class.
	 */
	protected StageFactory() {
	}

	/**
	 * Get the object of the factory class.
	 * 
	 * @return the object of the factory class.
	 */
	public static StageFactory getInstance() {
		return instance;
	}

	/**
	 * Create the Stage from the name.
	 * 
	 * @param name
	 *            the name of the Stage.
	 * @return the created Stage.
	 */
	public Stage createStage(String name) {
		return new Stage(name, mapStage.get(name));
	}

	/**
	 * Get the array of all stage's name.
	 * 
	 * @return the array of all stage's name.
	 */
	public String[] getAllStage() {
		Iterator<String> iter = mapStage.keySet().iterator();
		List<String> list = new ArrayList<String>();
		while (iter.hasNext()) {
			list.add((String) iter.next());
		}
		Collections.sort(list);
		String[] allStage = new String[mapStage.size()];
		list.toArray(allStage);
		return allStage;
	}

	/**
	 * Create and return the two dimensional array of the rectangle. (1 for the
	 * perimeter, the others are nothing.)
	 * 
	 * @param x
	 *            the width of the rectangle.
	 * @param y
	 *            the length of the rectangle.
	 * @return the two dimensional array of the rectangle with x and y area.
	 */
	public static Integer[][] createRectangle(int x, int y) {
		Integer[][] temp = new Integer[x][y];
		for (int i = 0; i < x; i++) {
			for (int j = 0; j < y; j++) {
				if (i == 0 || j == 0 || i == x - 1 || j == y - 1)
					temp[i][j] = 1;
				else if (i == 1 || j == 1 || i == x - 2 || j == y - 2)
					temp[i][j] = 1;
				else
					temp[i][j] = 0;
			}
		}
		removeCorner(temp, x, y);
		return temp;
	}

	/**
	 * Remove the corner of the rectangle from the array of the rectangle and
	 * add more data.(11,22,33,44 for the curves of four corners, 2 for the goal
	 * line, 3 for the normal surface which is not include in the race)
	 * 
	 * @param x
	 *            the width of the rectangle.
	 * @param y
	 *            the length of the rectangle.
	 * @return the two dimensional array of the rectangle with x and y area.
	 */

	public static void removeCorner(Integer[][] blocks, int y, int x) {
		blocks[0][0] = 11;
		blocks[0][1] = 2;
		blocks[1][0] = 3;
		blocks[1][1] = 2;
		blocks[0][x - 1] = 22;
		blocks[0][x - 2] = 3;
		blocks[1][x - 1] = 3;
		blocks[1][x - 2] = 3;
		blocks[y - 1][0] = 33;
		blocks[y - 1][1] = 3;
		blocks[y - 2][0] = 3;
		blocks[y - 2][1] = 3;
		blocks[y - 1][x - 1] = 44;
		blocks[y - 1][x - 2] = 3;
		blocks[y - 2][x - 1] = 3;
		blocks[y - 2][x - 2] = 3;
	}
}
